package com.es.easystoragesimulatedata.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.es.easystoragesimulatedata.mapper.StationInfoMapper;
import com.es.easystoragesimulatedata.model.StationInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电站信息服务类
 * 
 * @author EasyStorage
 * @since 2025-01-22
 */
@Slf4j
@Service
public class StationInfoService {

    @Autowired
    private StationInfoMapper stationInfoMapper;

    /**
     * 保存电站信息
     */
    public boolean saveStationInfo(StationInfo stationInfo) {
        try {
            int result = stationInfoMapper.insert(stationInfo);
            log.info("保存电站信息成功，影响行数: {}", result);
            return result > 0;
        } catch (Exception e) {
            log.error("保存电站信息失败: {}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 根据电站ID查询电站信息
     */
    public StationInfo getStationInfoByStationId(String stationId) {
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id", stationId);
        return stationInfoMapper.selectOne(queryWrapper);
    }

    /**
     * 根据电站类型查询电站信息
     */
    public List<StationInfo> getStationInfoByType(String stationType) {
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_type", stationType)
                   .orderByDesc("create_time");
        return stationInfoMapper.selectList(queryWrapper);
    }

    /**
     * 分页查询电站信息
     */
    public Page<StationInfo> getStationInfoPage(int current, int size) {
        Page<StationInfo> page = new Page<>(current, size);
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return stationInfoMapper.selectPage(page, queryWrapper);
    }

    /**
     * 更新电站信息
     */
    public boolean updateStationInfo(StationInfo stationInfo) {
        try {
            int result = stationInfoMapper.updateById(stationInfo);
            log.info("更新电站信息成功，影响行数: {}", result);
            return result > 0;
        } catch (Exception e) {
            log.error("更新电站信息失败: {}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 删除电站信息（逻辑删除）
     */
    public boolean deleteStationInfo(Long id) {
        try {
            int result = stationInfoMapper.deleteById(id);
            log.info("删除电站信息成功，影响行数: {}", result);
            return result > 0;
        } catch (Exception e) {
            log.error("删除电站信息失败: {}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 获取所有电站信息
     */
    public List<StationInfo> getAllStationInfo() {
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return stationInfoMapper.selectList(queryWrapper);
    }

    /**
     * 根据电站名称模糊查询
     */
    public List<StationInfo> getStationInfoByName(String stationName) {
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("station_name", stationName)
                   .orderByDesc("create_time");
        return stationInfoMapper.selectList(queryWrapper);
    }

    /**
     * 根据电站状态查询
     */
    public List<StationInfo> getStationInfoByStatus(String status) {
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status)
                   .orderByDesc("create_time");
        return stationInfoMapper.selectList(queryWrapper);
    }

    /**
     * 获取所有电站状态统计
     */
    public long getStationCountByStatus(String status) {
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        return stationInfoMapper.selectCount(queryWrapper);
    }
}
